gg.listeninpalstic = true;
gg.stopactionflag = false;
Plastic = cc.Node.extend({
	ctor:function(p){
		this._super();
		p.addChild(this, 10, TAG_PLASTIC);
		this.init();
	},
	init : function(){
		this.setCascadeOpacityEnabled(true);
		var plastic= new Button(this, 9, TAG_PLASTIC1, "#yiqi/plasticplate.png",this.callback);//铁片
	
		plastic.setScale(1.2);
		plastic.setPosition(1000,180);
		
		var leftline= new Button(plastic, 10, TAG_PLASTIC_LINE, "#yiqi/line14.png",this.callback);//横绳
		leftline.setVisible(false);
		leftline.setRotation(-90);
		leftline.setAnchorPoint(0.5,0);
		leftline.setPosition(cc.p(plastic.width*0.15,plastic.height*0.5));
		
		var rightline= new Button(plastic, 10, TAG_PLASTIC_LINE1, "#yiqi/line14.png",this.callback);
		rightline.setVisible(false);		
		rightline.setRotation(-90);
		rightline.setAnchorPoint(0.5,1);
		rightline.setPosition(cc.p(plastic.width*0.85,plastic.height*0.5));
		
		var arrow = new Button(plastic, 10, TAG_PLASTIC_ARROW, "#yiqi/arrow14.png",this.callback)//箭头
		arrow.setVisible(false);		
		arrow.setRotation(-90);
		arrow.setAnchorPoint(1,0.5);
		arrow.setPosition(cc.p(plastic.width*0.55,0));
	},
	
	addListener:function(thing){

		cc.eventManager.addListener(
				cc.EventListener.create({
					event: cc.EventListener.TOUCH_ONE_BY_ONE,
					onTouchBegan: this.onTouchBegan,
					onTouchMoved: this.onTouchMoved,
					onTouchEnded: this.onTouchEnded,
					onTouchCancelled: this.onTouchEnded
				}), thing);

	},
	onTouchBegan:function(touch, event){
		//cc.log("onTouchBegan");
		var plastic = event.getCurrentTarget();//获取点击
		var pos = touch.getLocation();
		var rect = plastic.getBoundingBox();
		if(cc.rectContainsPoint(rect, pos)&&!gg.listeninpalstic){//力不在同一直线
			gg.listeninhoocode = true;
			gg.listeninpalstic = true;
			var leftline = ll.run.getChildByTag(TAG_PLASTIC).getChildByTag(TAG_PLASTIC1).getChildByTag(TAG_PLASTIC_LINE);
			var rightline = ll.run.getChildByTag(TAG_PLASTIC).getChildByTag(TAG_PLASTIC1).getChildByTag(TAG_PLASTIC_LINE1);
			var lefthookcode = ll.run.getChildByTag(TAG_CODE).getChildByTag(TAG_CODE1);
			var righthookcode = ll.run.getChildByTag(TAG_CODE).getChildByTag(TAG_CODE2);
			var leftpulleyline = ll.run.getChildByTag(TAG_PULLEY).getChildByTag(TAG_PULLEY_LINE);
			var rightpulleyline = ll.run.getChildByTag(TAG_PULLEY).getChildByTag(TAG_PULLEY_LINE1);
			var l1 = leftline.getPositionX();
			var l2 = rightline.getPositionX();
			var l = (l2-l1)/2;
			var h = Math.sin(35*Math.PI/180)*l;
			var xr = 925-pos.x;
			var xl = pos.x-425;
			var angleL = Math.atan(h/xl)*180/Math.PI+35;
			var angleR = Math.atan(h/xr)*180/Math.PI+35;
			var rotate = cc.rotateBy(1,-35);
			var rotate1 = cc.rotateBy(1,angleL);
			var rotate2 = cc.rotateBy(1,angleR);
			plastic.runAction(rotate);
			leftline.runAction(cc.spawn(rotate1));   //var scale = cc.scaleTo(0.5,0.8,0.85);
			rightline.runAction(cc.spawn(rotate2));
			plastic.canMove = true;
			var arrow = ll.run.getChildByTag(TAG_PLASTIC).getChildByTag(TAG_PLASTIC1).getChildByTag(TAG_PLASTIC_ARROW);
			arrow.setVisible(false);
			ll.tip.arr.setVisible(false);
			gg.stopactionflag = true;
		};
		return true;
	},
	onTouchMoved:function(touch, event){
		
		var plastic = event.getCurrentTarget();
		var leftline = ll.run.getChildByTag(TAG_PLASTIC).getChildByTag(TAG_PLASTIC1).getChildByTag(TAG_PLASTIC_LINE);
		var rightline = ll.run.getChildByTag(TAG_PLASTIC).getChildByTag(TAG_PLASTIC1).getChildByTag(TAG_PLASTIC_LINE1);
		var lefthookcode = ll.run.getChildByTag(TAG_CODE).getChildByTag(TAG_CODE1);
		var righthookcode = ll.run.getChildByTag(TAG_CODE).getChildByTag(TAG_CODE2);
		var leftpulleyline = ll.run.getChildByTag(TAG_PULLEY).getChildByTag(TAG_PULLEY_LINE);
		var rightpulleyline = ll.run.getChildByTag(TAG_PULLEY).getChildByTag(TAG_PULLEY_LINE1);
		if(plastic.canMove){
			var pos = touch.getLocation();
			var mpos = pos;
			if(mpos.y>750)//上边界约束
				mpos.y=750;
			var h = 575 - mpos.y;
			var xl = mpos.x - 425;
			var xr = 925 - mpos.x;
			var l1 = (Math.sqrt(xl*xl+h*h)-2*10*h/xl) / 250 * 0.85;//leftline长度，10为定滑轮大致半径
			var l2 = (Math.sqrt(xr*xr+h*h)-2*10*h/xr) / 250 * 0.85;//rightline长度
			var pl1 = null;//左定滑轮line长度
			var pl2 = null;//右定滑轮line长度
			var lposx = null;
			var lposy = null;
			var rposx = null;
			var rposy = null;
			if(mpos.y>=575){//无需减掉线在定滑轮内部长度
				l1 = Math.sqrt(xl*xl+h*h) / 250 * 0.85;
				l2 = Math.sqrt(xr*xr+h*h) / 250 * 0.85;
			}
			var bta = Math.atan(h / xl) * 180 / Math.PI;//leftline与水平方向夹角，逆时针方向
			var arf = Math.atan(h / xr) * 180 / Math.PI;//rightline与水平方向夹角
			//dx,dy最低点坐标
			var dx = 675;
			var dy = 575 - Math.sqrt((0.5*1.4*250/0.85+Math.sqrt(0.5*0.5*1.4*1.4*250*250/0.85/0.85+4*125*10))*(0.5*1.4*250/0.85+Math.sqrt(0.5*0.5*1.4*1.4*250*250/0.85/0.85+4*125*10))-250*250);
			if(l1>=1.4&&mpos.x>425){
				//左线长约束
				l1 = 1.4;
				var lcos = l1 * 250 * Math.cos(bta*Math.PI/180) / 0.85;
				var lsin = l1 * 250 * Math.sin(bta*Math.PI/180) / 0.85;
				var dl1 = 2 * 10 * Math.sin(bta * Math.PI / 180);
				if(mpos.y<575){
					mpos.x = lcos + 425 + dl1*Math.cos(bta*Math.PI / 180);
					mpos.y = 575 - lsin - dl1*Math.sin(bta*Math.PI / 180);
				}else{
					mpos.x = lcos + 425;
					mpos.y = 575 - lsin;
				}
				if(mpos.y>750)
					mpos.y=750;
				if(mpos.y<dy)
					if((mpos.x-425)/(575-mpos.y)<(dx-425)/(575-dy))
						{
							mpos.x = dx;
							mpos.y = dy;
							bta = Math.atan((575-dy)/(dx-425))*180/Math.PI;
						}
				xr = 925 - mpos.x;
				h = 575 - mpos.y;
				arf = Math.atan(h/xr) * 180 / Math.PI;
				var dl2 = 2 * 10 * Math.sin(arf * Math.PI / 180);
				l2 = (Math.sqrt(xr * xr + h *h) - dl2) / 250 * 0.85;
				if(mpos.y>575)
					l2 = Math.sqrt(xr * xr + h * h) / 250 * 0.85;
			}
			if(l2>=1.4){
				//右线长约束
				l2 = 1.4;
				var rcos = l2 * 250 * Math.cos(arf*Math.PI/180) / 0.85;
				var rsin = l2 * 250 * Math.sin(arf*Math.PI/180) / 0.85;
				mpos.y = 575 - rsin;
				mpos.x = 925 - rcos;
				var dl2 = 2*10*Math.sin(arf*Math.PI/180);
				if(mpos.y<575){
					mpos.x = mpos.x - dl2 * Math.cos(arf*Math.PI/180);
					mpos.y = mpos.y - dl2 * Math.sin(arf*Math.PI/180);
				}
				if(mpos.y>750)
					mpos.y=750;
				if(mpos.y<dy)
					if((925-mpos.x)/(575-mpos.y)<(925-dx)/(575-dy))
					{
						mpos.x = dx;
						mpos.y = dy;
						arf = Math.atan((575-dy)/(925-dx))*180/Math.PI;
					}
				xl = mpos.x - 425;
				h = 575 - mpos.y;
				bta = Math.atan(h/xl) * 180 / Math.PI;
				var dl1 = 2*10*Math.sin(bta*Math.PI/180);
				l1 = (Math.sqrt(xl*xl+h*h)-dl1) / 250 * 0.85;
				if(mpos.y>575)
					l1 = Math.sqrt(xl*xl+h*h) / 250 * 0.85;
			}
			pl1 = 0.65 + (0.85-l1);
			pl2 = 0.65 + (0.85-l2);
			lposx = 355;
			rposx = 995;
			lposy = -(0.85-l1)*210/0.85 + 372.7;
			rposy = -(0.85-l2)*210/0.85 + 372.7;
			if(gg.stopactionflag&&(mpos.y<565||mpos.y>580)){
				plastic.stopAllActions();
				leftline.stopAllActions();
				rightline.stopAllActions();
				plastic.setRotation(0);
				leftline.setRotation(-90+bta);//角度定位
				rightline.setRotation(-90-arf);
			}
			plastic.setPosition(mpos.x,mpos.y);//坐标定位
			lefthookcode.setPosition(lposx,lposy);
			righthookcode.setPosition(rposx,rposy);
			leftline.setScaleY(l1);//长度定位
			rightline.setScaleY(l2);
			leftpulleyline.setScaleY(pl1);
			rightpulleyline.setScaleY(pl2);
		}

	},
	onTouchEnded:function(touch, event){
		var plastic = event.getCurrentTarget();
		var arrow = ll.run.getChildByTag(TAG_PLASTIC).getChildByTag(TAG_PLASTIC1).getChildByTag(TAG_PLASTIC_ARROW);
		var leftline = ll.run.getChildByTag(TAG_PLASTIC).getChildByTag(TAG_PLASTIC1).getChildByTag(TAG_PLASTIC_LINE);
		var rightline = ll.run.getChildByTag(TAG_PLASTIC).getChildByTag(TAG_PLASTIC1).getChildByTag(TAG_PLASTIC_LINE1);
		var lefthookcode = ll.run.getChildByTag(TAG_CODE).getChildByTag(TAG_CODE1);
		var righthookcode = ll.run.getChildByTag(TAG_CODE).getChildByTag(TAG_CODE2);
		var leftpulleyline = ll.run.getChildByTag(TAG_PULLEY).getChildByTag(TAG_PULLEY_LINE);
		var rightpulleyline = ll.run.getChildByTag(TAG_PULLEY).getChildByTag(TAG_PULLEY_LINE1);
		if(gg.stopactionflag){
			plastic.stopAllActions();
			leftline.stopAllActions();
			rightline.stopAllActions();
		}
		gg.stopactionflag = false;
		var pos = plastic.getPosition();
		var h  = 575 - pos.y;
		var xl = pos.x - 425;
		var xr = 925 - pos.x;
		var arf = Math.atan(h / xr) * 180 / Math.PI;
		var bta = Math.atan(h / xl) * 180 / Math.PI;
		var gama = ((180- (arf+bta))/2-(90-arf))*Math.PI/180;//合成力角度，余弦定理
		var mposx = pos.x-h*Math.tan(gama);
		var movep = cc.moveTo(1,cc.p(mposx,575));
		xl = mposx - 425;
		xr = 925 - mposx;
		var l1 = xl / 250 * 0.85;
		var l2 = xr / 250 * 0.85;
		var rotate = cc.rotateTo(1.0,-90);
		var rotateP = cc.rotateTo(1,0);
		var scaleL = cc.scaleTo(0.6,0.8,l1);
		var scaleR = cc.scaleTo(0.6,0.8,l2);
		if(pos.y>575){
			scaleL = cc.scaleTo(0.9,0.8,l1);
			scaleR = cc.scaleTo(0.9,0.8,l2);
			rotate = cc.rotateTo(1.05,-90);
		}
		var pl1 = 0.65 + (0.85-l1);
		var pl2 = 0.65 + (0.85-l2);
		var scalePL = cc.scaleTo(1,0.8,pl1);
		var scalePR = cc.scaleTo(1,0.8,pl2);
		var lposx = 355;
		var rposx = 995;
		var lposy = -(0.85-l1)*210/0.85 + 372.7;
		var rposy = -(0.85-l2)*210/0.85 + 372.7;
		var moveBL = cc.moveTo(1,lposx,lposy);
		var moveBR = cc.moveTo(1,rposx,rposy);
		var seqlineL = cc.spawn(rotate.clone(),scaleL);
		var seqlineR = cc.spawn(rotate.clone(),scaleR);
		var seqlineBL = cc.sequence(scalePL);
		var seqlineBR = cc.sequence(scalePR);
		var seqBL = cc.sequence(moveBL);
		var seqBR = cc.sequence(moveBR);
		var seq = cc.sequence(cc.spawn(movep,rotateP),cc.callFunc(function(){
			arrow.setVisible(true);
			ll.tip.arr.setVisible(true);
			gg.listeninpalstic = false;
			gg.listeninhoocode = false;
		},this));
		if(plastic.canMove){
			leftline.runAction(seqlineL);
			rightline.runAction(seqlineR);
			leftpulleyline.runAction(seqlineBL);
			rightpulleyline.runAction(seqlineBR);
			lefthookcode.runAction(seqBL);
			righthookcode.runAction(seqBR);
			plastic.runAction(seq);
		}
		//cc.log("onTouchEnded");
		plastic.canMove = false;
	},
	
	leftlineAction:function(){
		var leftline = this.getChildByTag(TAG_PLASTIC1).getChildByTag(TAG_PLASTIC_LINE);	
		leftline.setVisible(true);
		leftline.setScale(0.8,0.1);
		var scale = cc.scaleTo(0.5,0.8,0.90);
		var seq = cc.sequence(scale);
		leftline.runAction(seq);
	},
	rightlineAction:function(){
		var rightline = this.getChildByTag(TAG_PLASTIC1).getChildByTag(TAG_PLASTIC_LINE1);
		rightline.setVisible(true);
		rightline.setScale(0.8,0.1);
		var scale = cc.scaleTo(0.5,0.8,0.92);
		var seq = cc.sequence(scale);
		rightline.runAction(seq);
	},
	
	plasActToLeft:function(){
		var plastic = this.getChildByTag(TAG_PLASTIC1);
		var move = cc.moveTo(1,505,575);
		plastic.runAction(move);
	},
	plasActToRight:function(){
		var plastic = this.getChildByTag(TAG_PLASTIC1);
		var move = cc.moveTo(1,835,575);
		plastic.runAction(move);
	},
	leftlineScaleS:function(){
		var leftline = this.getChildByTag(TAG_PLASTIC1).getChildByTag(TAG_PLASTIC_LINE);
		var scale = cc.scaleTo(1,0.8,0.3);
		var seq = cc.sequence(scale);
		leftline.runAction(seq);
	},
	leftlineScaleL:function(){
		var leftline = this.getChildByTag(TAG_PLASTIC1).getChildByTag(TAG_PLASTIC_LINE);
		var scale = cc.scaleTo(1,0.8,1.5);
		var seq = cc.sequence(scale);
		leftline.runAction(seq);
	},
	rightlineScaleS:function(){
		var rightline = this.getChildByTag(TAG_PLASTIC1).getChildByTag(TAG_PLASTIC_LINE1);
		var scale = cc.scaleTo(1,0.8,0.3);
		var seq = cc.sequence(scale);
		rightline.runAction(seq);
	},
	rightlineScaleL:function(){
		var rightline = this.getChildByTag(TAG_PLASTIC1).getChildByTag(TAG_PLASTIC_LINE1);
		var scale = cc.scaleTo(1,0.8,1.5);
		var seq = cc.sequence(scale);
		rightline.runAction(seq);
	},
	callback:function(p){
		var action = gg.flow.flow.action;
		var func=cc.callFunc(function(){
			gg.flow.next();
		},this);
		switch(p.getTag()){
		case TAG_PLASTIC1:
				var seq = cc.sequence(cc.spawn(cc.moveTo(1,675,575)),
						cc.callFunc(function(){	
							this.leftlineAction();
							this.rightlineAction();
							ll.run.pulley.leftlineAction();
							ll.run.pulley.rightlineAction();
						},this),cc.delayTime(1),func);
			p.runAction(seq);
			break;
		}
	},
});